package cn.fuxi2022.module.contact.service.impl;

import cn.fuxi2022.core.utils.PageUtils;
import cn.fuxi2022.core.utils.Query;
import cn.fuxi2022.model.empty.applets.GroupContact;
import cn.fuxi2022.module.contact.mapper.GroupContactMapper;
import cn.fuxi2022.module.contact.service.GroupContactService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 社员联系表（供学生添加微信和群聊） 服务实现类
 * </p>
 *
 * @author fuxi
 * @since 2022-10-10 12:56:19
 */
@Service
public class GroupContactServiceImpl extends ServiceImpl<GroupContactMapper, GroupContact> implements GroupContactService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 1.构建搜索条件
        LambdaQueryWrapper<GroupContact> wrapper = new LambdaQueryWrapper<>();
        // 按照创建日期排序
        wrapper.orderByDesc(GroupContact::getGmtCreate);

        // 2.获取请求参数，判断是否进行搜索查询
        // 搜索标题或描述
        if (ObjectUtils.isNotEmpty(params.get("search"))) {
            String search = String.valueOf(params.get("search"));
            wrapper.like(GroupContact::getTitle,search).or().like(GroupContact::getDescription,search);
        }

        // 搜索状态
        if (ObjectUtils.isNotEmpty(params.get("status"))) {
            int status = Integer.parseInt(String.valueOf(params.get("status")));

            if(status == 0){
                wrapper.eq(GroupContact::getStatus,0);
            } else if (status == 1) {
                wrapper.eq(GroupContact::getStatus,1);
            }
        }

        // 搜索类型
        if (ObjectUtils.isNotEmpty(params.get("type"))) {
            int status = Integer.parseInt(String.valueOf(params.get("type")));

            if(status == 0){
                wrapper.eq(GroupContact::getType,0);
            } else if (status == 1) {
                wrapper.eq(GroupContact::getType,1);
            } else if (status == 2) {
                wrapper.eq(GroupContact::getType,2);
            } else if (status == 3) {
                wrapper.eq(GroupContact::getType,3);
            } else if (status == 4) {
                wrapper.eq(GroupContact::getType,4);
            }
        }

        // 3.工具分页
        IPage<GroupContact> page = this.page(
                new Query<GroupContact>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }
}
